/*
 * Copyright (C) 2013 Fabiano Tarlao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.easybenchmark.client.benchmarks;

import java.util.LinkedList;
import java.util.Random;

/**
 * Benchmark object initialization as well as huge list creation and deletion, String contatenation and so on.
 * It's quite a mess, it has been the first experimental benchmark, it sucks.
 * @author fab
 */
public class ListBenchmarks extends Benchmark {
    private Random randGen = new Random();
    private int benchmarkWeight = 10000;

    public ListBenchmarks() {
        super();
    }
    
    /**
     * The benchamark is done on array of given benchmarkWeight size
     * @param benchmarkWeight the size of test array
     */
    public ListBenchmarks(int benchmarkWeight) {
        super();
        this.benchmarkWeight=benchmarkWeight;
    }
        
    @Override
    protected long benchTask() {
        int deltaRandom =randGen.nextInt(benchmarkWeight*2);
        LinkedList<UselessStuff> testList = new LinkedList<UselessStuff>();        
        int i;
        for (i = 0; i < benchmarkWeight; i++) {
            testList.add(new UselessStuff(i+deltaRandom));
        }
        
        //Empty List
        int startPoint = randGen.nextInt(benchmarkWeight*2);
        //Delete stuff, but left a little amount of  elements in order to stress GC
        for (i = 0; i < 8*benchmarkWeight/10; i++) {
            testList.remove(startPoint % testList.size());
        }        
        
        //return testList related result in order to avoid agressive optimizzations
        return(testList.size()+i);
    }

    @Override
    protected int initBenchTask() {
        return(0); //do nothing
    }

    @Override
    protected int initWarmUpTask() {
        return(0);
    }
    
    private class UselessStuff {
        //let's do  a bit of  Sting manipulation
        public UselessStuff(int id) {
            this.id=id;
            this.zeus = this.zeus.concat(String.valueOf(id));
        }
        private int id;
        private int giano =5;
        public String zeus ="Zeus is a God but is a nice motherboard too Zeus is a God but is a nice motherboard too Zeus is a God but is a nice motherboard too";
    }
    
}
